package com.katze.tools.hadoop.entity;

import com.katze.common.DatePattern;
import lombok.Data;
import org.apache.commons.io.FilenameUtils;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.protocol.HdfsFileStatus;

@Data
public class FileResource {

    private String name;

    private String extension;
    private String location;

    private String path;

    private long length;

    private boolean isDirectory;

    private String modifyTime;

    private String permissions;

    private String owner;

    private short replication;

    private long blockSize;

    private String group;

    public FileResource(FileStatus status) {
        Path file = status.getPath();
        this.name = file.getName();
        this.path = file.toUri().getPath();
        int index = this.path.lastIndexOf(Path.SEPARATOR);
        this.location = index == 0? "":this.path.substring(0, index);
        this.permissions = status.getPermission().toString();
        this.modifyTime = DatePattern.NORM_DATETIME_FORMAT.format(status.getModificationTime());
        this.length = status.getLen();
        this.isDirectory = status.isDirectory();
        this.owner = status.getOwner();
        this.replication = status.getReplication();
        this.blockSize = status.getBlockSize();
        this.group = status.getGroup();
        this.extension = this.isDirectory? "folder":FilenameUtils.getExtension(this.name);
    }

    public FileResource(HdfsFileStatus status) {
        this((FileStatus) status);
    }
}
